<?php

namespace haibao\www\business;

use haibao\www\data\Activity as ActivityData; 
use haibao\frame\data\query\Condition;

class Activity extends \haibao\www\business\Base{
    public static  function saveActivityPrizeInfo($activityPrizeWinnerData){
        $activityData = new ActivityData();
        $activityData->add($activityPrizeWinnerData);
    }
    
    public static function getActivityCache(){
    	$cache = new \haibao\www\cache\Activity();
    	$activityData = new ActivityData();
    	$activityCache = array();
    	$activityCache = $cache->getActivityCache();
    	if(!$activityCache){
    		$activityFilter = new \haibao\cms\model\filter\activity\Activity();
    		$activityFilter->select(array(
    				$activityFilter::CONDITION_FIELD_NAME_ID,
    				$activityFilter::CONDITION_FIELD_NAME_TITLE,
    				$activityFilter::CONDITION_FIELD_NAME_PRONUM,
    				$activityFilter::CONDITION_FIELD_NAME_PROTYPE,
    		));
    		$activityFilter->where($activityFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\activity\Activity::STATUS_ENABLE);
    		$nowTime = date('Y-m-d H:i:s',time());
    		$activityFilter->where($activityFilter::CONDITION_FIELD_NAME_STARTTIME, Condition::CONDITION_LESSEQUAL, $nowTime);
    		$activityFilter->where($activityFilter::CONDITION_FIELD_NAME_ENDTIME, Condition::CONDITION_GREATEREQUAL, $nowTime);
    
    		$getActivity = $activityData->getAll($activityFilter);
    		if($getActivity){
    			foreach($getActivity as $value){
    				$activityCache[$value->Id]['Title'] = $value->Title;
    				$activityCache[$value->Id]['ProNum'] = $value->ProNum;
    				$activityCache[$value->Id]['ProType'] = $value->ProType;
    			}
    			$cache->setActivityCache($activityCache);
    		}
    	}
    	return $activityCache;
    }
    
    public static function getActivityKeywordCache($activityId){
    	$cache = new \haibao\www\cache\Activity();
    	$activityData = new ActivityData();
    	$activityKeywordCache = array();
    	$activityKeywordCache = $cache->getActivityKeywordCache($activityId);
    	if(!$activityKeywordCache){
    		$activityKeywordFilter = new \haibao\cms\model\filter\activity\ActivityKeyword();
    		$activityKeywordFilter->select(array(
    				$activityKeywordFilter::CONDITION_FIELD_NAME_NAME
    		));
    		$activityKeywordFilter->where($activityKeywordFilter::CONDITION_FIELD_NAME_ACTIVITYID, Condition::CONDITION_EQUAL, $activityId);
    		$activityKeywordFilter->where($activityKeywordFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\activity\ActivityKeyword::STATUS_ENABLE);
    		$today = date('Y-m-d',time());
    		$activityKeywordFilter->where($activityKeywordFilter::CONDITION_FIELD_NAME_SHOWTIME, Condition::CONDITION_EQUAL, $today);
    		$getActivityKeyword = $activityData->getAll($activityKeywordFilter);
    		if($getActivityKeyword){
	    		foreach($getActivityKeyword as $keyword){
	    			$activityKeywordCache[] = $keyword->Name;
	    		}
    			$cache->setActivityKeywordCache($activityId,$activityKeywordCache);
    		}
    	}
    	return $activityKeywordCache;
    }
    
    public static function getActivityPrizeCache($activityId){
    	$cache = new \haibao\www\cache\Activity();
    	$activityData = new ActivityData();
    	$activityPrizeCache = array();
    	$activityPrizeCache = $cache->getActivityKeywordCache($activityId,false);
    	if(!$activityPrizeCache){
    		$activityPrizeFilter = new \haibao\cms\model\filter\activity\ActivityPrize();
    		$activityPrizeFilter->select(array(
    				$activityPrizeFilter::CONDITION_FIELD_NAME_ID,
    				$activityPrizeFilter::CONDITION_FIELD_NAME_DESC,
    				$activityPrizeFilter::CONDITION_FIELD_NAME_LEVEL,
    				$activityPrizeFilter::CONDITION_FIELD_NAME_RESTNUM,
    				$activityPrizeFilter::CONDITION_FIELD_NAME_IMAGEID
    		));
    		$activityPrizeFilter->where($activityPrizeFilter::CONDITION_FIELD_NAME_ACTIVITYID, Condition::CONDITION_EQUAL, $activityId);
    		$activityPrizeFilter->where($activityPrizeFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\www\model\data\activity\ActivityPrize::STATUS_ENABLE);
    		$activityPrizeFilter->where($activityPrizeFilter::CONDITION_FIELD_NAME_RESTNUM, Condition::CONDITION_GREATER, 0);
    		$getActivityPrize = $activityData->getAll($activityPrizeFilter);
    		if($getActivityPrize){
    			foreach ($getActivityPrize as $prize){
    				$activityPrizeCache[$prize->Id]['RestNum'] = $prize->RestNum;
    				$activityPrizeCache[$prize->Id]['ImageId'] = $prize->ImageId;
    				$activityPrizeCache[$prize->Id]['Desc'] = $prize->Desc;
    				$activityPrizeCache[$prize->Id]['Level'] = $prize->Level;
    			}
    			$cache->setActivityKeywordCache($activityId, $activityPrizeCache,false);
    		}
    	}
    	return $activityPrizeCache;
    }
    
    public static function delActivityCache(){
    	$cache = new \haibao\www\cache\Activity();
    	$cache->delActivityCache();
    }
    
    public static function delActivityInfoCache($activityId,$isKeyword=true){
    	$cache = new \haibao\www\cache\Activity();
    	$cache->delActivityInfoCache($activityId,$isKeyword);
    }
    
    public static function addSearchKeywordNum($key){
    	$activityData = new ActivityData();
    	$activityData->addSearchKeywordNum($key);
    }
    
    public static function checkPrize($activityId,$prizeId,$isGet=false){
    	$activityData = new ActivityData();
    	return $activityData->checkPrize($activityId,$prizeId,$isGet);
    }
}